package com.kuqi.fragments;

import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.kuqi.R;
import com.kuqi.adapter.BookStoreGridAdapter;
import com.kuqi.adapter.PromotionHolderAdapter;
import com.kuqi.presenters.BookStorePresenter;

// 书城界面
public class BookStoreFragment extends Fragment {

    // 绑定显示界面
    private View rootView;

    // 促销的轮播图
    private ConvenientBanner promotion;
    // 言情
    private RelativeLayout rl_love_view;
    private TextView tv_love_header_text;
    private GridView gv_love;
    // 穿越
    private RelativeLayout rl_travel_view;
    private TextView tv_travel_header_text;
    private GridView gv_travel;
    // 名著
    private RelativeLayout rl_master_view;
    private TextView tv_master_header_text;
    private GridView gv_master;
    // 网游
    private RelativeLayout rl_game_view;
    private TextView tv_game_header_text;
    private GridView gv_game;

    // 功能代理
    private BookStorePresenter presenter;

    // 空构造
    public BookStoreFragment() {
        presenter = new BookStorePresenter(getContext(),getLifecycle(),msgHandler);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_book_store, container, false);
        rl_love_view = rootView.findViewById(R.id.rl_love_view);
        tv_love_header_text = rootView.findViewById(R.id.tv_love_header_text);
        gv_love = rootView.findViewById(R.id.gv_love);
        rl_travel_view = rootView.findViewById(R.id.rl_travel_view);
        tv_travel_header_text = rootView.findViewById(R.id.tv_travel_header_text);
        gv_travel = rootView.findViewById(R.id.gv_travel);
        rl_master_view = rootView.findViewById(R.id.rl_master_view);
        tv_master_header_text = rootView.findViewById(R.id.tv_master_header_text);
        gv_master = rootView.findViewById(R.id.gv_master);
        rl_game_view = rootView.findViewById(R.id.rl_game_view);
        tv_game_header_text = rootView.findViewById(R.id.tv_game_header_text);
        gv_game = rootView.findViewById(R.id.gv_game);
        promotion = rootView.findViewById(R.id.banner_promotion);

        // 设置字体样式
        Typeface tf = Typeface.createFromAsset(getActivity().getAssets(), "fonts/fangsong.TTF");
        tv_love_header_text.setTypeface(tf);
        tv_travel_header_text.setTypeface(tf);
        tv_master_header_text.setTypeface(tf);
        tv_game_header_text.setTypeface(tf);

        // 获取基础数据 准备渲染
        presenter.searchStoreData();

        return rootView;
    }


    /**
     * 数据处理的msgHandler回调函数
     */
    private Handler msgHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == BookStorePresenter.FINISH_STORE_SEARCH) {
                // 初始查询数据完成，回调进行渲染
                initShowComponent();
            }
        }
    };


    /**
     * 初始化几个分组的显示内容
     */
    private void initShowComponent()
    {
        // 为一众GridView设置显示适配器,使其在点击后可以做出指定反映,跳转指定界面
        gv_love.setAdapter(new BookStoreGridAdapter(getActivity(), presenter.loveList));
        gv_travel.setAdapter(new BookStoreGridAdapter(getActivity(), presenter.travelList));
        gv_master.setAdapter(new BookStoreGridAdapter(getActivity(), presenter.masterList));
        gv_game.setAdapter(new BookStoreGridAdapter(getActivity(), presenter.gameList));

        // 为一众书籍刊头设置点击事件，点击展开或者收起
        rl_love_view.setOnClickListener(foldClickListener);
        rl_travel_view.setOnClickListener(foldClickListener);
        rl_master_view.setOnClickListener(foldClickListener);
        rl_game_view.setOnClickListener(foldClickListener);

        // 打折促销轮播图
        promotion.setPages(new CBViewHolderCreator<PromotionHolderAdapter>() {
            @Override
            public PromotionHolderAdapter createHolder() {
                return new PromotionHolderAdapter();
            }
        }, presenter.promotionImages);
        promotion.setPointViewVisible(true); //是否显示小圆点
        promotion.startTurning(2000); // 切换时间间隔
    }


    /**
     * TODO 展示列表展开和收起点击功能
     */
    private View.OnClickListener foldClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(v.getId() == rl_love_view.getId()){
                Log.e("bookstore", "点击言情小说的收起和展开");
            }else if(v.getId() == rl_travel_view.getId()){
                Log.e("bookstore", "点击穿越小说的收起和展开");
            }else if(v.getId() == rl_master_view.getId()){
                Log.e("bookstore", "点击名著小说的收起和展开");
            }
        }
    };



}
